package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.rfid.project.exception.CannotDelException;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class DeliveryWayService{

	@Autowired
	private DeliveryWayDao deliveryWayDao;
	private ProjectDeliveryDao projectDeliveryDao;
	
	private QDeliveryWay $ = QDeliveryWay.deliveryWay;
	private QProjectDelivery $_ = QProjectDelivery.projectDelivery;
	
	
	@Autowired
	public void setDeliveryWayDao(DeliveryWayDao deliveryWayDao) {
		this.deliveryWayDao = deliveryWayDao;
	}
	@Autowired
	public void setProjectDeliveryDao(ProjectDeliveryDao projectDeliveryDao) {
		this.projectDeliveryDao = projectDeliveryDao;
	}
	
	
	
	public List<DeliveryWay> findAll(){
		return ListUtil.toList(deliveryWayDao.findAll());
	}
				
	public List<DeliveryWay> findAll(
	String name,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(deliveryWayDao.findAll(condition));
	}		

	public Page<DeliveryWay> findAll(
			String name,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(StringUtil.isNotEmpty(name)){
				condition.and($.name.contains(name));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
	
			/*
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			*/
			
			
			Sort sortObject_1 = new Sort(Direction.DESC, "orderNumber");
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject_1);
			return deliveryWayDao.findAll(condition, pageRequest);
	}
	
	
	
	public DeliveryWay getDeliveryWay(Long id){
		return deliveryWayDao.getOne(id);
	}
	
	//public void deleteDeliveryWay(Long id){
	//	deliveryWayDao.delete(id);
	//}
	
	public void deleteDeliveryWay(Long id) throws CannotDelException{
		if (this.findDeliveryWay(id)) {
			throw new CannotDelException("该方式已经被项目交付物引用，请先删除项目交付物或修改项目交付物的引用！");
		} else {
			this.deliveryWayDao.delete(id);
		}
	}
	private boolean findDeliveryWay(Long deliveryWayId){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_.deliveryWay.id.eq(deliveryWayId));
		return this.projectDeliveryDao.count(condition) > 0;
	}
	

	public void saveDeliveryWay(DeliveryWay deliveryWay) {
		
		if (null == deliveryWay.getOrderNumber())
			deliveryWay.setOrderNumber(this.getOrderNumber());
		
		deliveryWayDao.save(deliveryWay);
	}
	
	private int getOrderNumber(){
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<DeliveryWay> deliveryWays = this.deliveryWayDao.findAll(condition,pageRequest);
		if(deliveryWays.getTotalElements() > 0){
			DeliveryWay deliveryWay = deliveryWays.getContent().get(0);
			return deliveryWay.getOrderNumber()+1;
		}
		return 1;
	}

	
	
	public boolean changeOrderNumber(Long id, Long targetId){
		DeliveryWay currentModule = this.deliveryWayDao.findOne(id);
		DeliveryWay targetModule = this.deliveryWayDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.deliveryWayDao.save(currentModule);
		this.deliveryWayDao.save(targetModule);
		return true;
	}
	
	public DeliveryWay findOne(Long id) {
		return this.deliveryWayDao.findOne(id);
	}
	

}
		
